import logging

from aiogram import types
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.types import InputFile

import client
import client_kb
import handlers.users.generate.mx_country
import sqlite_db
from main import dp

logger = logging.getLogger(__name__)


async def delete_user_reply(user_id: int, message_id: int):
    try:
        await dp.bot.delete_message(chat_id=user_id, message_id=message_id)
    except Exception as e:
        logger.warning(f"Unable to delete user message: {e}")


async def command_create_menu_button(message: types.Message):
    """
    Обработка нажатия на кнопку назад для возврата в меню выбора страны
    """
    user_id = message.from_user.id
    await sqlite_db.sql_check_user(user_id)
    await message.answer("Выберите страну", reply_markup=client_kb.kb_counries)
    await message.answer("Выберите страну", reply_markup=client_kb.kb_counries)
    await client.FSM.create_menu.set()


class FSM_CMMX(StatesGroup):
    doc_type_select = State()


async def create_mx(message: types.Message):
    """Клавиатура с выбором желаемого документа."""
    await delete_user_reply(message.from_user.id, message.message_id)
    user_id = message.from_user.id
    await sqlite_db.sql_check_user(user_id)
    await message.answer("Выберите документ для создания", reply_markup=client_kb.kb_mx)
    await FSM_CMMX.doc_type_select.set()


@dp.message_handler(state=FSM_CMMX.doc_type_select)
async def doc_type(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    """Реагирование на выбранный документ и отправка примера заполнения с вызовом соответствующей функции."""
    if message.text == "Назад ◀️":
        # message_to_edit = await handlers.users.generate.kz_country.kz_passport.data_checkup(message.from_user.id, state)
        # await state.update_data(message_to_edit=message_to_edit)
        await command_create_menu_button(message)

    elif message.text == "BILL 🇲🇽":
        type_doc = "mx_bill"
        await sqlite_db.sql_add_country_user(message.from_user.id, "mx")
        await state.update_data(type=type_doc)
        await dp.bot.send_photo(
            chat_id=message.from_user.id,
            photo=InputFile("./files/mx_bill/bill_mexico1.png"),
        )
        await handlers.users.generate.mx_country.mx_bill.command_create_mx_bill(
            message, state
        )
    elif message.text == "PASS 🇲🇽":
        type_doc = "mx_pass"
        await sqlite_db.sql_add_country_user(message.from_user.id, "mx_pass")
        await state.update_data(type=type_doc)
        try:
            await dp.bot.send_photo(
                chat_id=message.from_user.id,
                photo=InputFile("./files/mx/mx_pass/example.jpg"),
            )
        except:
            pass
        await handlers.users.generate.mx_country.mx_passport.mx_pass_name_input(
            message, state
        )
    # elif message.text == "ID OLD 🇰🇿":
    #     type_doc = "kz_id_old"
    #     await sqlite_db.sql_add_country_user(message.from_user.id, "kz")
    #     await state.update_data(type=type_doc)
    #     try:
    #         await dp.bot.send_photo(
    #             chat_id=message.from_user.id, photo=InputFile("./files/kz/kz_id_old/example.png")
    #         )
    #     except:
    #         pass
    #     await handlers.users.generate.kz_country.kz_id_old.kz_id_old_language_input(message, state)
